/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dawnland02.model.entity;

import javafx.async.JavaTaskBase;
import javafx.reflect.FXClassType;
import javafx.reflect.FXLocal;
import javafx.reflect.FXLocal.Context;
import javafx.reflect.FXLocal.ObjectValue;
import javafx.reflect.FXObjectValue;

import dawnland02.model.quest.QuestManager;

/**
 * @author Petru Obreja (obrejap@yahoo.com)
 */
public class EntityFactory {
    
    private static Integer entityId = 1;

    public static Entity createEntity(Integer x, Integer y){
        EntityInternal entity = new EntityImpl(entityId++, x, y);
        entity.setJavaTaskBase(createEntityJavaTaskBase(entity));
        QuestManager.getQuestManager().attach(entity);
        entity.start();
        return entity;
    }

    private static JavaTaskBase createEntityJavaTaskBase(Entity entity){
        //load the javaFX class using reflection
        Context context = FXLocal.getContext();
        FXClassType instance = context.findClass("dawnland02.model.entity.EntityJavaTaskBase");
        ObjectValue objectValue = (ObjectValue)instance.newInstance();
        //init the javafx object
        Object obj = objectValue.asObject();
        //set the entity
        ((JavaTaskBaseInterface) obj).setEntity(entity);
        return (JavaTaskBase) obj;
    }
}
